package cn.tedu.rabbitmqboot.m2;


import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.PostConstruct;

@SpringBootApplication
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    public Queue taskQueue() {
        //spring默认是持久化的,true可以不写
        return new Queue("task_queue", true);
    }

    @Autowired
    Producer p;

    @PostConstruct //在Spring执行完扫描,创建完所有的对象,完成注入后,自动执行PostConstruct方法
    public void test() {
        /*
         * p.send()内有死循环,会阻塞Spring
         * 要启动一个新的线程执行send
         */
        /*
            new Thread(new Runnable() {
                @Override
                public void run() {
                    p.send();
                }
            }).start();
        */
        new Thread(() -> p.send()).start();//lambda表达式
    }
}

